package com.juyu.miliao.api.service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.juyu.miliao.api.dao.redis.CallTimeRedisDao;
import com.juyu.miliao.api.dao.redis.DiamondRedisDao;
import com.juyu.miliao.api.domain.vo.UserSortVo;
import com.juyu.miliao.common.constant.CallTimeRankingListKey;
import com.juyu.miliao.common.constant.DiamondRankingListKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.juyu.miliao.api.cache.GradeCache;
import com.juyu.miliao.api.dao.mysql.GradeDao;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.Grade;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.util.DayTypeUtil;
import com.juyu.miliao.api.util.GradeUtil;
import com.juyu.miliao.api.util.PageUtil;
import com.juyu.miliao.common.util.SumAgeUtil;

@Service
public class UserSortService {

	@Resource
	private RedisTemplateService redisTemplateService;
	@Resource
	private UserInfoDao userInfoDao;
	@Resource
	private GradeDao gradeDao;
	@Resource
	private FollowService followService;
	@Resource
	private CallTimeRedisDao callTimeRedisDao;
	@Resource
	private DiamondRedisDao diamondRedisDao;

	private final Logger logger = LoggerFactory.getLogger(UserSortService.class);

	/**
	 * @param tokenUserId 查询用户id
	 * @param type 1:魅力榜 2:贡献榜 3:时长榜
	 * @param day 1:日榜 2:周榜 3:总榜 4:昨日榜
	 * @param page 页号
	 * @param pageSize 每页显示条数
	 */
	public List<User> getUserSort(long tokenUserId,Integer type, Integer day, Integer page, Integer pageSize) {
		int startNum = PageUtil.getOffset(page, pageSize);
		String rediskey = DayTypeUtil.getDayType(type,day);
		List<Grade> mGradeList = GradeCache.INSTANCE.getGradeList(1);
		List<Grade> vGradeList = GradeCache.INSTANCE.getGradeList(2);

		Map<String, String> maps = redisTemplateService.getMUserSort(rediskey,startNum,pageSize*page-1);
		if(maps.size()==0)
			return null;

		List<User> userList = new ArrayList<User>();
		for (String mapkey : maps.keySet()) {
			User user=userInfoDao.getUserInfoById(Integer.parseInt(mapkey));

			//计算m、v等级
			if (user != null) {
				user.setmGrade(GradeUtil.getGradeCount(mGradeList, user.getmGradeSum()));
				user.setvGrade(GradeUtil.getGradeCount(vGradeList, user.getvGradeSum()));
				String followRelation = followService.getFollowRelation(user.getUserId(),tokenUserId);
				if(followRelation.equals("false")){
					user.setIsFollow(2);
				}else {
					user.setIsFollow(1);
				}
				//生日转换成年龄
				if(user.getBirthday()!=null)
					user.setAge(SumAgeUtil.getAgeByBirth(user.getBirthday()));
				//redis中的值存入bean返回前端
				if(type == 1)
					user.setmGradeSum(Integer.parseInt(maps.get(mapkey)));
				else
					user.setvGradeSum(Integer.parseInt(maps.get(mapkey)));

				userList.add(user);
			}
		}
		return userList;
	}

	/**
	 *
	 * @param tokenUserId
	 * @param type 1:魅力榜 2:贡献榜 3:时长榜
	 * @param day 1:日榜 2:周榜 3:总榜 4:昨日榜
	 * @return
	 */
    public long getMySort(long tokenUserId, Integer type, Integer day) {
		String rediskey = DayTypeUtil.getDayType(type,day);
		Long temp = redisTemplateService.getUserSortReverseRank(tokenUserId,rediskey);
		if (temp == null){
			temp = 0L;
		}else{
			temp++;
		}
		return temp;
    }
	/**
	 * @param type 1:时长榜 2:钻石榜
	 * @param day 1:昨日 2:今日 3:本周 4:上周
	 * @param page 页号
	 * @param pageSize 每页显示条数
	 * @return
	 */
	public List<UserSortVo> getHostUserSort(long tokenUserId, Integer type, Integer day, Integer page, Integer pageSize) {
		int startNum = PageUtil.getOffset(page, pageSize);
		List<UserSortVo> dayRankingList = new ArrayList<>();
		if(type==1){
			if(day==1) {
				 dayRankingList = callTimeRedisDao.getDayRankingList(LocalDate.now().minusDays(1), startNum, pageSize * page - 1);
			}else if(day==2){
				dayRankingList = callTimeRedisDao.getDayRankingList(LocalDate.now(), startNum, pageSize * page - 1);
			}else if(day == 3){
				dayRankingList = callTimeRedisDao.getWeekRankingList(LocalDate.now(),startNum, pageSize * page - 1);
			}else if(day == 4){
				dayRankingList = callTimeRedisDao.getWeekRankingList(LocalDate.now().minusWeeks(1),startNum, pageSize * page - 1);
			}
		}else if(type==2){
			if(day==1) {
				dayRankingList = diamondRedisDao.getDayRankingDiamondList(LocalDate.now().minusDays(1), startNum, pageSize * page - 1);
			}else if(day==2){
				dayRankingList = diamondRedisDao.getDayRankingDiamondList(LocalDate.now(), startNum, pageSize * page - 1);
			}else if(day == 3){
				dayRankingList = diamondRedisDao.getWeekRankingDiamondList(LocalDate.now(),startNum, pageSize * page - 1);
			}else if(day == 4){
				dayRankingList = diamondRedisDao.getWeekRankingDiamondList(LocalDate.now().minusWeeks(1),startNum, pageSize * page - 1);
			}
		}

		return dayRankingList;
	}
	/**
	 *
	 * @param tokenUserId
	 * @param type 1:时长榜 2:钻石榜
	 * @param day 1:昨日 2:今日 3:本周 4:上周
	 * @return
	 */
	public long getMyHostSort(long tokenUserId, Integer type, Integer day) {
		String rediskey = null;

		if(type==1){
			if(day==1){
				rediskey = CallTimeRankingListKey.getDayRankingListKey(LocalDate.now().minusDays(1));
			}else if(day==2){
				rediskey = CallTimeRankingListKey.getDayRankingListKey(LocalDate.now());
			}else if(day==3){
				rediskey = CallTimeRankingListKey.getWeekRankingListKey(LocalDate.now());
			}else if(day==4){
				rediskey = CallTimeRankingListKey.getWeekRankingListKey(LocalDate.now().minusWeeks(1));
			}


		}else if(type==2){
			if(day==1){
				rediskey = DiamondRankingListKey.getDayRankingListKey(LocalDate.now().minusDays(1));
			}else if(day==2){
				rediskey = DiamondRankingListKey.getDayRankingListKey(LocalDate.now());
			}else if(day==3){
				rediskey = DiamondRankingListKey.getWeekRankingListKey(LocalDate.now());
			}else if(day==4){
				rediskey = DiamondRankingListKey.getWeekRankingListKey(LocalDate.now().minusWeeks(1));
			}
		}
		Long temp = redisTemplateService.getUserSortReverseRank(tokenUserId,rediskey);
		if (temp == null){
			temp = 0L;
		}else{
			temp++;
		}
		return temp;
	}
}
